<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||	
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws. 
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.). 
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations). 
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Account/classes/PhpFox_ComponentFlagUpload.class.php, $Author:  $ */



/** Component for uploading new country flags
 *   
 * @version $Id: PhpFox_ComponentFlagUpload.class.php 46 2007-12-14 23:12:21Z phpfoxsv $
 * @package module.account
 */
class PhpFox_Mod_Account_ComponentFlagUpload extends BaseComponent
{
    function process()
    {
        $oSecurityService = $this->_oModule->getService('Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        App::loadClass('file/Image');
        $oImage = &new file_image();
        $oReq = &App::getRequest();
        $oTpl = &$this->_oModule->getTemplate();
        if (!$oSecurityService->hasStaffPerm('MENU_flag_database'))
        {
            App::gotoUrl('admin.main');
        }

        $sFlagPath = App::getParam('path').'file/flag/';
        $aFlags = array();
        foreach ($oImage->readDir($sFlagPath, 'gif') as $sFile => $bIsDir)
        {
            if (!$bIsDir)
            {
                $aFlags[] = str_replace('.gif', '', $sFile);
            }
        }
        $aCountriesWFlag = array();
        foreach (App::getTxtFile('country.txt') as $sCountry)
        {
            if (!in_array($sCountry, $aFlags))
            {
                $aCountriesWFlag[$sCountry] = $sCountry;
            }
        }
        if ($sCountry = $oReq->get('country'))
        {
            if (!in_array($sCountry, $aCountriesWFlag))
            {
                App::gotoUrl('admin.extra.flagdb');
            }
            if ($oReq->getAction() == 'upload')
            {
                $oImage->aLimits['FILE_MAX_SIZE'] = App::getParam('flag.max_size');
                $oImage->aLimits['MAX_IMAGE_W']   = App::getParam('flag.max_w');
                $oImage->aLimits['MAX_IMAGE_H']   = App::getParam('flag.max_h');
                $oImage->aLimits['MIN_IMAGE_W']   = App::getParam('flag.min_w');
                $oImage->aLimits['MIN_IMAGE_H']   = App::getParam('flag.min_h');
                if (!$oImage->upload('image', $sFlagPath, $sCountry, 'gif'))
                {
                    $aParams = array
                    (
                        'country' => $sCountry,
                    );
                    $aErrors = $oImage->getErrors();
                    $aErrors[] = App::format('Mod_Account.flag.not_uploaded');
                    App::gotoUrl('admin.extra.flagdb', $aParams, $aErrors, true, 'upload');
                }
                App::gotoUrl('admin.extra.flagdb', array(), App::format('Mod_Account.flag.uploaded'), false, 'upload');
            }
            $oTpl->assign(array(
                'aCountries' => $aCountriesWFlag,
                'sCountry'   => $sCountry,
                'bUploading' => true,
            ));
            return $oTpl->fetch('FlagUpload.html');
        }
        $oTpl->assign(array(
            'aCountries' => $aCountriesWFlag,
            'iCnt'       => count($aCountriesWFlag),
        ));
        return $oTpl->fetch('FlagUpload.html');
    }
}
?>